package com.monicash.monicash.login;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.monicash.monicash.R;
import com.monicash.monicash.base.BaseActivity;
import com.monicash.monicash.login.modle.LoginModle;
import com.monicash.monicash.util.T;

public class LoginActivity extends BaseActivity {

    private TextView back;
    private TextView mTvButton;
    private TextView mTvLength;
    private EditText mEtPhone;
    private TextView mTvAreaNumber;
    private TextView mTvAccountShow;
    private ImageView mIvFlag;
    private LinearLayout mLlaccount;
    private LinearLayout mLlPassword;
    private ImageView mIvLookUp;
    private EditText mEtPassword;
    private TextView mTvForgotPassword;

    private LinearLayout mLlVerify;

    private ImageView mIvVerify;

    private EditText mEtValidate;

    private ImageView mIvValidate;

    private ImageView mIvRefreshVerify;

    private String account = "";
    private String password = "";
    private int errorNum = 0;
    String yesPassword = "123456";
    private boolean look_up = false;

    private boolean isAccount = false;
    LoginModle loginModle = null;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        loginModle = LoginModle.Companion.getInstant();
        initView();
        initData();
    }

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    public void initView() {
        mTvButton = findViewById(R.id.tv_button);
        mTvLength = findViewById(R.id.tv_length);
        mEtPhone = findViewById(R.id.et_phone);
        mTvAreaNumber = findViewById(R.id.tv_area_number);
        mIvFlag = findViewById(R.id.iv_flag);
        mTvAccountShow = findViewById(R.id.tv_account_show);
        back = findViewById(R.id.back);

        mLlaccount = findViewById(R.id.ll_account);

        mLlPassword = findViewById(R.id.ll_password);
        mIvLookUp = findViewById(R.id.iv_look_up);
        mEtPassword = findViewById(R.id.et_password);
        mTvForgotPassword = findViewById(R.id.tv_forgot_password);

        mLlVerify = findViewById(R.id.ll_verify);
        mIvVerify = findViewById(R.id.iv_verify);
        mIvValidate = findViewById(R.id.iv_validate);
        mIvRefreshVerify = findViewById(R.id.iv_refresh_verify);
        mEtValidate = findViewById(R.id.et_validate);

        if(look_up){
            mEtPassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
        }else {
            mEtPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        }

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mTvButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!isAccount){
                    account = mEtPhone.getText().toString();
                    if(TextUtils.isEmpty(account)){
                        isAccount = false;
                        T.showShort(getActivity(),"请输入账号");
                        return;
                    }
                    if(!account.startsWith("9")){
                        isAccount = false;
                        T.showShort(getActivity(),"手机号格式不正确");
                        return;
                    }
                    isAccount = true;
                    String data = loginModle.getLoginMiLuPageData("56",account);

//                    mTvAccountShow.setText(String.format("Hi,%s",account));
//                    mLlaccount.setVisibility(View.GONE);
//                    mLlPassword.setVisibility(View.VISIBLE);
                }else {
                    String data = loginModle.getLoginMiLuPageData("56",account);
                    password = mEtPassword.getText().toString();
                    if(TextUtils.equals(password,yesPassword)){
                        //密码正确，进入下一步

//                        Intent intent = new Intent(LoginActivity.this, VerifyActivity.class);
//                        intent.putExtra("account",account);
//                        startActivity(intent);
//                        finish();
                    }else{
                        //密码错误，清空密码继续输入
                        errorNum++;
                        password = "";
                        mEtPassword.setText(password);
                        T.showShort(getActivity(),"密码错误");
                        if(errorNum > 2){
                            mLlVerify.setVisibility(View.VISIBLE);
                        }
                    }
                }
            }
        });

        mTvForgotPassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, ResetPasswordActivity.class);
                startActivity(intent);

            }
        });

        mIvLookUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                look_up = !look_up;
                if(look_up){
                    mEtPassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                }else {
                    mEtPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);

                }
            }
        });

        mEtPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(mEtPhone.getText()!=null){
                    String ss = String.format("%s/9", s.length());
                    mTvLength.setText(ss);
                }
            }
        });


    }

    @Override
    public void initData() {

    }
}
